package cm.mefobe.android.telecomcost.view;

import java.util.ArrayList;
import java.util.Calendar;

import cm.mefobe.android.telecomcost.adapter.DayAdapter;
import cm.mefobe.android.telecomcost.entities.Day;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;

public class DaysCheckListView extends ListView {

	private Context _context;
	private ArrayList<Day> day_list;
	private DayAdapter _adapter;

	public DaysCheckListView(Context context) {
		super(context);
		_context = context;
	}

	public DaysCheckListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;
	}

	public DaysCheckListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		_context = context;
	}

	private void setList() throws Exception {
		if (_context != null) {
			day_list = new ArrayList<Day>();
			day_list.add(new Day(Calendar.MONDAY));
			day_list.add(new Day(Calendar.TUESDAY));
			day_list.add(new Day(Calendar.WEDNESDAY));
			day_list.add(new Day(Calendar.THURSDAY));
			day_list.add(new Day(Calendar.FRIDAY));
			day_list.add(new Day(Calendar.SATURDAY));
			day_list.add(new Day(Calendar.SUNDAY));

			_adapter = new DayAdapter(_context,
					android.R.layout.select_dialog_multichoice, day_list);
		} else {
			throw new Exception(
					"cannot set the adapter of the TimePeriodSpinner...the context is null");
		}
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		Log.i("DaysCheckListView", "setting the adapter...");
		try {
			setList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.setAdapter(_adapter);
	}

	public ArrayList<Day> getSelections() {
		ArrayList<Day> days = new ArrayList<Day>();

		int index = 0;
		while (index < day_list.size()) {
			Day day = day_list.get(index);
			if (day.isSelected()) {
				days.add(day);
			}

			index++;
		}

		return days;
	}

	public ArrayList<Day> getSelectedDays() {
		 return getSelections();		
	}
}
